package com.meijm.pipeline;

import com.alibaba.fastjson.JSON;
import com.meijm.entity.BoHaiShiBei;
import com.meijm.util.HTMLSpirit;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class BHSBPipeline implements Pipeline {
	private final  String baseDir = "d://crawler//博海拾贝//";
	private String saveDir = "";

	public void process(ResultItems resultItems, Task task) {
		BoHaiShiBei bhsb = JSON.parseObject(resultItems.get("com.meijm.entity.BoHaiShiBei").toString(), BoHaiShiBei.class);
		if(bhsb.getTitle().indexOf("博海拾贝")>-1){
			saveDir = baseDir+ HTMLSpirit.filter(bhsb.getTitle())+"//";
			File dir = new File(saveDir);
			if(!dir.exists()){
				dir.mkdir();
			}
			List<String> titles = bhsb.getImgTitle();
			List<String> srcs = bhsb.getImgSrc();
			int i = 0;
			for(String title:titles){
				if(StringUtils.isNotBlank(title)){
					if(i<srcs.size()){
						String imgsrc = srcs.get(i);
						int point = imgsrc.lastIndexOf(".");
						title = HTMLSpirit.filter(title);
						String fileName = title+imgsrc.substring(point);
						URL l_url = null;
						try {
							l_url = new URL(imgsrc);
							File file=new File(saveDir+fileName);
							FileUtils.copyURLToFile(l_url, file);
						} catch (MalformedURLException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}
						i++;
					}
				}
			}
		}
	}
}
